package panda.query.tree;

import panda.query.condition.BooleanExpression;
import panda.query.scan.Scanner;
import panda.query.scan.SelectScanner;
import panda.query.struct.Attribute;
import panda.record.Schema;
import panda.transaction.Transaction;

/**
 * 
 * @author Tian Yuan
 */
public class SelectNode implements QueryTreeNode {
	
	QueryTreeNode p;
	BooleanExpression c;
	Transaction tx;
	
	public SelectNode(QueryTreeNode p, BooleanExpression c, Transaction tx) {
		this.p = p;
		this.c = c;
		this.tx = tx;
	}
	
	public Scanner open() {
		return new SelectScanner(p.open(), c, tx);
	}

	public int blockAccessed() {
		return p.blockAccessed();
	}

	public int distinctSize(Attribute attr) {
		int res;
		if ((res = c.equateWithConstant(attr)) > 0)
			return res;
		else
			return Math.min(p.distinctSize(attr), outputSize());
	}

	public int outputSize() {
		return (int)(p.outputSize() * c.reductionFactor(p));
	}

	public Schema getSchema() {
		return p.getSchema();
	}

}
